﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using OrbitOne.Elmah.Logic;

namespace OrbitOne.Elmah.Presenter
{
    public class Utility
    {
        #region Constants
        private const string CodeIcon = "icon_code.gif";
        private const string InfrastructureIcon = "icon_infrastructure.gif";
        private const string MessageIcon = "icon_errorLevel1.gif"; //TODO Rename the images
        private const string WarningIcon = "icon_errorLevel2.gif";
        private const string ErrorIcon = "icon_errorLevel3.gif";
        private const string SearchFiltersProfileGroup = "SearchFilters";
        private const string InternalUserIcon = "icon_homeUser.gif";
        private const string AnonymousIcon = "icon_anonymous.gif";
        private const string WebbotIcon = "icon_webbots.gif";
        private const string UserIcon = "icon_user.gif";
        #endregion

        #region Static Methods
        public static string BreakLongUrl(string str, int numberOfChars)
        {
            if (str.Length > numberOfChars)
            {
                str = str.IndexOf("/") != -1
                    ? str.Insert(str.IndexOf("/") + 1, " ")
                    : str.Insert(numberOfChars, " ");
            }
            return str;
        }

        public static string GetErrorLevelImageName(int errorLevel)
        {
            string result;
            switch (errorLevel)
            {
                case 0:
                case 1:
                    result = MessageIcon;
                    break;
                case 2:
                case 3:
                    result = WarningIcon;
                    break;
                default:
                    result = ErrorIcon;
                    break;
            }
            return result;
        }

        public static string GetUserImageName(OrbitOneElmahErrorFacade.UserTypeEnum error)
        {
            string result;
            switch (error)
            {
                case OrbitOneElmahErrorFacade.UserTypeEnum.Known:
                    result = UserIcon;
                    break;
                case OrbitOneElmahErrorFacade.UserTypeEnum.Bot:
                    result = WebbotIcon;
                    break;
                case OrbitOneElmahErrorFacade.UserTypeEnum.InternalUser:
                    result = InternalUserIcon;
                    break;
                default:
                    result = AnonymousIcon;
                    break;
            }
            return result;
        }

        public static string GetErrorTypeImageName(string errorType)
        {
            var type = (ErrorSearchFilter.ErrorTypeEnum)Enum.Parse(typeof(ErrorSearchFilter.ErrorTypeEnum), errorType);

            string result;
            switch (type)
            {
                case ErrorSearchFilter.ErrorTypeEnum.Infrastructure:
                    result = InfrastructureIcon;
                    break;

                default:
                    result = CodeIcon;
                    break;
            }

            return result;
        }

        public static void SaveSearchFilter(HttpContext context, string profileName, ErrorSearchFilter searchFilter)
        {
            var groupBase = context.Profile.GetProfileGroup(SearchFiltersProfileGroup);
            groupBase.SetPropertyValue(profileName, searchFilter);
        }

        public static ErrorSearchFilter GetSearchFilter(HttpContext context, string profileName)
        {
            var groupBase = context.Profile.GetProfileGroup(SearchFiltersProfileGroup);
            return groupBase.GetPropertyValue(profileName) as ErrorSearchFilter;
        }

        public static ErrorSearchFilter GetCurrentSearchFilter(HttpContext context)
        {
            var groupBase = context.Profile.GetProfileGroup(SearchFiltersProfileGroup);
            return groupBase.GetPropertyValue("DefaultSearchFilter") as ErrorSearchFilter;
        }

        public static IList<ErrorSearchFilter> GetAllSearchFilters(HttpContext Context)
        {
            var filters = new List<ErrorSearchFilter>();
            var groupBase = Context.Profile.GetProfileGroup(SearchFiltersProfileGroup);
            for (var i = 1; i <= 10; i++)
            {
                var filter = groupBase[string.Format("SearchFilter{0}", i)] as ErrorSearchFilter;
                if (filter != null && !string.IsNullOrEmpty(filter.Name))
                {
                    filters.Add(filter);
                }

            }
            return filters;
        }
        #endregion
    }
    public static class Extensions
    {
        public static bool IsNotNullOrEmpty(this string source)
        {
            return !string.IsNullOrEmpty(source);
        }
        public static T ParseEnum<T>(this string source)
        {
            return (T) Enum.Parse(typeof (T), source);
        }
        
        public static T EnumToObject<T>(this int source)
        {
            return (T)Enum.ToObject(typeof(T), source);
        }
        public static T CombinedEnumValues<T>(this ImageCheckBoxList collection)
        {
            var combinedValues = 0;

            collection.Items.Cast<ListItem>()
                            .Where(item => item.Selected)
                            .ToList()
                            .ForEach(item => combinedValues = combinedValues + item.Value.ParseEnumToInt<T>());

            return combinedValues.EnumToObject<T>();
        }
        private static int ParseEnumToInt<T>(this string source)
        {
            return (int)Enum.Parse(typeof(T), source);
        }
    }
}
